/*
 * Created by JFormDesigner on Mon Dec 21 21:23:17 CST 2020
 */

package cn.fourhundredandeighteen.view.StudentView.studentSelectCourswInfo;

import cn.fourhundredandeighteen.controller.StudentController;
import cn.fourhundredandeighteen.utill.Result;
import cn.fourhundredandeighteen.view.Component.Button;
import cn.fourhundredandeighteen.view.Component.Container;
import cn.fourhundredandeighteen.view.StudentView.StudentCourseInfo.studentCourseInfo;
import cn.fourhundredandeighteen.view.StudentView.StudentFunction.StudentSelectCourswInfo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author root
 */
public class studentSelectCourswInfo extends JPanel {
    public StudentSelectCourswInfo studentSelectCourswInfo = new StudentSelectCourswInfo();
    public Long studentSession;
    public JScrollPane studentSelectCourswInfo1;
    StudentController studentController = new StudentController();
    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JButton button1;

    public studentSelectCourswInfo(Long studentSession) {
        this.studentSession = studentSession;
        initComponents();
        getdata();

    }

    public void getdata() {
        studentSelectCourswInfo1 = studentSelectCourswInfo.StudentSelectCourswInfo(studentSession);
        this.add(studentSelectCourswInfo1);
        this.updateUI();
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        try {
            Result result = studentController.studentSelectCourse(Long.valueOf(studentCourseInfo.newText), studentSession);
            if (result.getMassage() == "SUCCESS") {
                this.remove(studentSelectCourswInfo1);
                getdata();
                JOptionPane.showConfirmDialog(null, "选课成功", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
                Container.getInstance().upDatePage();
            } else {
                JOptionPane.showConfirmDialog(null, "选课失败", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);

            }
        } catch (NumberFormatException error) {
            JOptionPane.showConfirmDialog(null, "请输入课程号", "提示", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);

        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();

        button1 = new Button();

        //======== this ========
        setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout(FlowLayout.CENTER, 26, 15));


            //---- button1 ----
            button1.setText("\u9009\u8bfe");
            button1.addActionListener(e -> button1ActionPerformed(e));
            panel1.add(button1);
        }
        add(panel1, BorderLayout.SOUTH);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
